package concurrecy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class ThrowExceptionTask implements Runnable {

    private int count = 0;

    @Override
    public void run() {
        while(count ++ < 3)
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(count);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        throw new RuntimeException("Runnable 抛出异常");
    }
}

public class UncaughtExceptionDemo {
    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool(r -> {
            Thread t = new Thread(r);
            t.setUncaughtExceptionHandler((thread, e) -> {
                System.out.println(thread + " : " + e.getMessage());
            });
            return t;
        });
        service.execute(new ThrowExceptionTask());
        service.shutdown();
    }
}
